<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of als_solve_rx</title>
  <meta name="keywords" content="als_solve_rx">
  <meta name="description" content="Computes an approximate low-rank solution for 2D case">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html v1.5 &copy; 2003-2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="#">tt2</a> &gt; <a href="index.html">solve</a> &gt; als_solve_rx.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for tt2/solve&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>als_solve_rx
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>Computes an approximate low-rank solution for 2D case</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function [x]=als_solve_rx(mat, rhs, tol, drx, nswp, addswp) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">Computes an approximate low-rank solution for 2D case
   [x]=ALS_SOLVE_RX(MAT, RHS, [TOL], [RX], [NSWP])
   Finds a solution to 2D TTM matrix MAT using the ALS to a 2D TT tensor
   with rank rx, but the RHS and X are represented as full vectors.
   TOL is the tolerance for ||x_{i+1}-x_i||/||x_i||,
   DRX is the random kick rank,
   NSWP - number of ALS sweeps.
   default values:
   tol: 1e-12
   rx: 1
   nswp: 10


 TT-Toolbox 2.2, 2009-2012

This is TT Toolbox, written by Ivan Oseledets et al.
Institute of Numerical Mathematics, Moscow, Russia
webpage: http://spring.inm.ras.ru/osel

For all questions, bugs and suggestions please mail
ivan.oseledets@gmail.com
---------------------------</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="../../tt2/@qtt_tucker/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>	Frobenius norm of the QTT-Tucker</li><li><a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>	Complex conjugate of a TT-matrix</li><li><a href="../../tt2/@tt_matrix/norm.html" class="code" title="function [nrm] = norm(t,varargin)">norm</a>	Matrix norm of the TT-matrix</li><li><a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>	Mode sizes of the TT-matrix</li><li><a href="../../tt2/@tt_tensor/conj.html" class="code" title="function [tt1]=conj(tt)">conj</a>	Compute a complex conjugate of TT-tensor</li><li><a href="../../tt2/@tt_tensor/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>	Frobenius norm of the TT-tensor</li><li><a href="../../tt2/@tt_tensor/qr.html" class="code" title="function [tt,rm]=qr(tt,op)">qr</a>	Left and right orthogonalization of the TT-format</li><li><a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>	Reshape of the TT-tensor</li><li><a href="../../tt2/@tt_tensor/size.html" class="code" title="function [sz] = size(tt,dim)">size</a>	Mode sizes of the TT-tensor</li><li><a href="../../tt2/core/tt_mat_full_vec.html" class="code" title="function [y]=tt_mat_full_vec(tt,x)">tt_mat_full_vec</a>	Multiplication of the TT-matrix by full vector in TT1.0 format</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="als_solve_rx_2.html" class="code" title="function [x]=als_solve_rx_2(mat, rhs, tol, maxit, x0, drx, nswp)">als_solve_rx_2</a>	Computes an approximate low-rank solution for 2D case (Method 2)</li></ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [x]=als_solve_rx(mat, rhs, tol, drx, nswp, addswp)</a>
0002 <span class="comment">%Computes an approximate low-rank solution for 2D case</span>
0003 <span class="comment">%   [x]=ALS_SOLVE_RX(MAT, RHS, [TOL], [RX], [NSWP])</span>
0004 <span class="comment">%   Finds a solution to 2D TTM matrix MAT using the ALS to a 2D TT tensor</span>
0005 <span class="comment">%   with rank rx, but the RHS and X are represented as full vectors.</span>
0006 <span class="comment">%   TOL is the tolerance for ||x_{i+1}-x_i||/||x_i||,</span>
0007 <span class="comment">%   DRX is the random kick rank,</span>
0008 <span class="comment">%   NSWP - number of ALS sweeps.</span>
0009 <span class="comment">%   default values:</span>
0010 <span class="comment">%   tol: 1e-12</span>
0011 <span class="comment">%   rx: 1</span>
0012 <span class="comment">%   nswp: 10</span>
0013 <span class="comment">%</span>
0014 <span class="comment">%</span>
0015 <span class="comment">% TT-Toolbox 2.2, 2009-2012</span>
0016 <span class="comment">%</span>
0017 <span class="comment">%This is TT Toolbox, written by Ivan Oseledets et al.</span>
0018 <span class="comment">%Institute of Numerical Mathematics, Moscow, Russia</span>
0019 <span class="comment">%webpage: http://spring.inm.ras.ru/osel</span>
0020 <span class="comment">%</span>
0021 <span class="comment">%For all questions, bugs and suggestions please mail</span>
0022 <span class="comment">%ivan.oseledets@gmail.com</span>
0023 <span class="comment">%---------------------------</span>
0024 
0025 
0026 a1 = mat{1}; a2 = mat{2};
0027 n1 = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(a1,1); m1 = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(a1,2);
0028 n2 = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(a2,1); m2 = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(a2,2);
0029 ra = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(a1,3);
0030 
0031 <span class="comment">% tol2 = 1e-3;</span>
0032 
0033 rhs = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(rhs, n1, n2);
0034 
0035 <span class="keyword">if</span> (nargin&lt;3)||(isempty(tol))
0036     tol=1e-12;
0037 <span class="keyword">end</span>;
0038 <span class="keyword">if</span> (nargin&lt;4)||(isempty(drx))
0039     drx=1;
0040 <span class="keyword">end</span>;
0041 <span class="keyword">if</span> (nargin&lt;5)||(isempty(nswp))
0042     nswp=10;
0043 <span class="keyword">end</span>;
0044 <span class="keyword">if</span> (nargin&lt;6)||(isempty(addswp))
0045     addswp=2;
0046 <span class="keyword">end</span>;
0047 
0048 <span class="keyword">if</span> (drx&gt;m1)||(drx&gt;m2)
0049     drx = min(m1,m2);
0050 <span class="keyword">end</span>;
0051 
0052 rx=1;
0053 
0054 curx = cell(2,1);
0055 curx{1}=rand(m1,rx);
0056 curx{2}=rand(m2,rx);
0057 x = curx{1}*(curx{2}.');
0058 derr = 2;
0059 sp = 0;
0060 resid_old = 1;
0061 <span class="keyword">for</span> swp=1:nswp
0062     
0063 
0064     <span class="comment">% QR 2-1</span>
0065     [q,rv]=<a href="../../tt2/@tt_tensor/qr.html" class="code" title="function [tt,rm]=qr(tt,op)">qr</a>(curx{2},0); <span class="comment">% m2,rx' - rx',rx</span>
0066     rx = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(q,2);
0067     curx{2}=q;
0068 <span class="comment">%     curx{1}=curx{1}*(rv.');</span>
0069     
0070     <span class="comment">% compute phi</span>
0071     a2 = permute(mat{2}, [1 3 2]);
0072     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a2, n2*ra, m2);
0073     phi = a2*curx{2}; <span class="comment">% size n2*ra, rx</span>
0074     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(phi, n2, ra*rx);
0075     phi = (phi')*phi; <span class="comment">% size ra*rx, ra*rx &lt;-- for cplx should also work</span>
0076     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(phi, ra, rx, ra, rx);
0077     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(phi, [1 3 2 4]), ra*ra, rx*rx);
0078 <span class="comment">%     phi = reshape(permute(phi, [3 1 2 4]), ra, ra*rx*rx);</span>
0079 <span class="comment">%     a2 = reshape(mat{2}, n2, m2*ra);</span>
0080 <span class="comment">%     phi = (a2.')*phi; % size m2*ra, ra*rx</span>
0081 <span class="comment">%     phi = reshape(phi, m2, ra*ra*rx);</span>
0082 <span class="comment">%     phi = (curx{2}.')*phi; % size rx, ra*ra*rx</span>
0083 <span class="comment">%     phi = reshape(permute(reshape(phi, rx, ra, ra, rx), [2 3 1 4]), ra*ra, rx*rx);</span>
0084 <span class="comment">%     % And the projection of the matrix</span>
0085     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(mat{1}, [3 2 1]), ra*m1, n1);
0086     a1 = <a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(a1)*<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(mat{1}, n1, m1*ra); <span class="comment">% size ra*m1, m1*ra &lt;-- conjugate!</span>
0087     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a1, ra, m1, m1, ra);
0088     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(a1, [1 4 2 3]), ra*ra, m1*m1);
0089 <span class="comment">%     a1 = reshape(permute(mat{1}, [3 2 1]), ra, m1*n1);</span>
0090     a1 = (phi.')*a1; <span class="comment">% size rx*rx, m1*n1</span>
0091     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a1, rx, rx, m1, m1);
0092     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(a1, [2 4 1 3]), rx*m1, rx*m1);
0093 <span class="comment">%     a1 = reshape(mat{1}, n1*m1, ra)*phi; % size n1*m1, ra*rx*rx</span>
0094 <span class="comment">%     a1 = reshape(a1, n1, m1, ra, rx, rx);</span>
0095 <span class="comment">%     a1 = reshape(permute(a1, [1 3 2 4 5]), n1*ra, m1*rx*rx);</span>
0096 <span class="comment">%     a1 = conj(reshape(permute(mat{1}, [2 1 3]), m1, n1*ra))*a1; % size m1, m1*rx*rx</span>
0097 <span class="comment">%     a1 = reshape(a1, m1, m1, rx, rx);</span>
0098 <span class="comment">%     a1 = reshape(permute(a1, [3 1 4 2]), rx*m1, rx*m1);</span>
0099     
0100     <span class="comment">%rhs:</span>
0101     
0102     rhs1 = rhs*<a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(mat{2}, n2, m2*ra)); <span class="comment">% size n1, m2*ra &lt;-- conjugate</span>
0103     rhs1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(rhs1, n1, m2, ra);
0104     rhs1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(rhs1, [1 3 2]), n1*ra, m2);
0105     
0106     rhs1 = <a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(mat{1}, [2 1 3]), m1, n1*ra))*rhs1; <span class="comment">% size m1, m2</span>
0107 <span class="comment">%     rhs1 = rhs;</span>
0108     rhs1 = rhs1*<a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(curx{2}); <span class="comment">% size m1,rx</span>
0109     rhs1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(rhs1.', rx*m1, 1);
0110     
0111     curx{1}=a1 \ rhs1; <span class="comment">% new first block</span>
0112 <span class="comment">%     cond_a1 = cond(a1)</span>
0113     curx{1}=<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(curx{1}, rx, m1).';
0114     
0115     <span class="comment">% Now, let's try the kickass by rank drx:</span>
0116     <span class="keyword">if</span> (mod(swp,addswp)==0)
0117 <span class="comment">%     if (sp&gt;5)</span>
0118         curx{1}=[curx{1}, randn(m1,drx)];
0119 <span class="comment">%         sp=0;</span>
0120     <span class="keyword">end</span>;
0121 <span class="comment">%     rx=rx+1;</span>
0122     
0123     <span class="comment">% Now, let's compute the second block</span>
0124     [q,rv]=<a href="../../tt2/@tt_tensor/qr.html" class="code" title="function [tt,rm]=qr(tt,op)">qr</a>(curx{1},0); <span class="comment">% m1,rx' - rx',rx</span>
0125     rx = <a href="../../tt2/@tt_matrix/size.html" class="code" title="function [sz] = size(tt)">size</a>(q,2);
0126     curx{1}=q;
0127     
0128     <span class="comment">% compute phi</span>
0129     a1 = permute(mat{1}, [1 3 2]);
0130     a1 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a1, n1*ra, m1);
0131     phi = a1*q; <span class="comment">% size n1*ra, rx</span>
0132     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(phi, n1, ra*rx);
0133     phi = (phi')*phi; <span class="comment">% size ra*rx, ra*rx</span>
0134     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(phi, ra, rx, ra, rx);
0135     phi = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(phi, [1 3 2 4]), ra*ra, rx*rx);    
0136 <span class="comment">%     a1 = reshape(mat{1}, n1, m1*ra);</span>
0137 <span class="comment">%     phi = (a1.')*phi; % size m1*ra, ra*rx</span>
0138 <span class="comment">%     phi = reshape(phi, m1, ra*ra*rx);</span>
0139 <span class="comment">%     phi = (curx{1}.')*phi; % size rx, ra*ra*rx</span>
0140 <span class="comment">%     phi = reshape(permute(reshape(phi, rx, ra, ra, rx), [2 3 1 4]), ra*ra, rx*rx);</span>
0141     <span class="comment">% And the projection of the matrix</span>
0142     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(mat{2}, [3 2 1]), ra*m2, n2);
0143     a2 = <a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(a2)*<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(mat{2}, n2, m2*ra); <span class="comment">% size ra*m2, m2*ra</span>
0144     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a2, ra, m2, m2, ra);
0145     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(a2, [1 4 2 3]), ra*ra, m2*m2);
0146 <span class="comment">%     a2 = reshape(permute(mat{2}, [3 2 1]), ra, m2*n2);</span>
0147     a2 = (phi.')*a2; <span class="comment">% size rx*rx, m2*n2</span>
0148     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(a2, rx, rx, m2, m2);
0149     a2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(a2, [2 4 1 3]), rx*m2, rx*m2);
0150     
0151     <span class="comment">%rhs:</span>
0152     rhs2 = rhs*<a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(mat{2}, n2, m2*ra)); <span class="comment">% size n1, m2*ra</span>
0153     rhs2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(rhs2, n1, m2, ra);
0154     rhs2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(rhs2, [1 3 2]), n1*ra, m2);
0155     
0156     rhs2 = <a href="../../tt2/@tt_matrix/conj.html" class="code" title="function [b]=conj(a)">conj</a>(<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(permute(mat{1}, [2 1 3]), m1, n1*ra))*rhs2; <span class="comment">% size m1, m2</span>
0157 <span class="comment">%     rhs2 = rhs;</span>
0158     rhs2 = (curx{1}')*rhs2; <span class="comment">% size rx,m2</span>
0159     rhs2 = <a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(rhs2, rx*m2, 1);
0160     
0161     curx{2}=a2 \ rhs2; <span class="comment">% new first block</span>
0162     curx{2}=<a href="../../tt2/@tt_tensor/reshape.html" class="code" title="function [tt2]=reshape(tt1,sz,eps, rl, rr)">reshape</a>(curx{2}, rx, m2).';
0163     
0164     x_new = curx{1}*(curx{2}.'); <span class="comment">% size m1,m2</span>
0165     derr = <a href="../../tt2/@qtt_tucker/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>(x_new(:)-x(:))/<a href="../../tt2/@qtt_tucker/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>(x(:));
0166     
0167     x = x_new;
0168     
0169     resid = <a href="../../tt2/@qtt_tucker/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>(<a href="../../tt2/core/tt_mat_full_vec.html" class="code" title="function [y]=tt_mat_full_vec(tt,x)">tt_mat_full_vec</a>(mat, x(:))-rhs(:))/<a href="../../tt2/@qtt_tucker/norm.html" class="code" title="function [nrm] = norm(tt)">norm</a>(rhs(:));
0170     conv_fact = resid_old/resid;
0171     <span class="keyword">if</span> (conv_fact-1&lt;1e-4)
0172         sp=sp+1;
0173     <span class="keyword">end</span>;
0174     
0175     fprintf(<span class="string">'als_solve: swp=%d, derr=%3.3e, rx=%d, resid=%3.3e, conv-1=%3.5e\n'</span>, swp, derr, rx, resid, conv_fact-1);
0176     <span class="keyword">if</span> (derr&lt;tol)
0177         <span class="keyword">break</span>;
0178     <span class="keyword">end</span>;
0179     resid_old = resid;
0180 <span class="keyword">end</span>;
0181 
0182 x = x(:);
0183 
0184 <span class="keyword">end</span></pre></div>
<hr><address>Generated on Wed 08-Feb-2012 18:20:24 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>